//
//  LEEStatuseCell.swift
//  SinaWeibo
//
//  Created by 李江波 on 2016/11/16.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit
import SDWebImage

private let margin : CGFloat = 8

private let imgMargin : CGFloat = 2
private let maxWidth : CGFloat = kUISCRRENW - 2 * margin
private let itemWidth : CGFloat = (maxWidth - 2 * imgMargin) / 3

@IBDesignable class LEEStatuseCell: UITableViewCell {

    @IBOutlet weak var avaterImg: UIImageView!
    
    @IBOutlet weak var name: UILabel!
    
    @IBOutlet weak var mbrank: UIImageView!
    
    @IBOutlet weak var create_time: UILabel!
    
    @IBOutlet weak var source: UILabel!
    
    @IBOutlet weak var lblText: YYLabel!
    
    @IBOutlet weak var avater_enterprise: UIImageView!
    
    @IBOutlet weak var picFlowLayout: UICollectionViewFlowLayout!
    
    @IBOutlet weak var picturesCollectionV: LEEPictureView!
    
    @IBOutlet weak var collectionViewWidth: NSLayoutConstraint!
    
    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
    
    @IBOutlet weak var pic_text_Margin: NSLayoutConstraint!
    
    @IBOutlet weak var retweetText: YYLabel!
    
    @IBOutlet weak var threeBtn: UIStackView!
    var itemModel : LEEStatuseViewModel? {
    
        didSet{
        
            avaterImg.sd_setImage(with: itemModel?.iconUrl)
            name.text = itemModel?.statuse?.user?.name
            create_time.text = itemModel?.sendTime
            source.text = itemModel?.weiboSource
            lblText.attributedText = itemModel?.originalText
            mbrank.image = itemModel?.mbmarkImage
            avater_enterprise.image = itemModel?.avatarImage
            
            //重新计算约束collectionView尺寸
            let count = itemModel?.imgUrls?.count ?? 0
            
            pic_text_Margin?.constant = (count == 0 ? 0 : margin)
            
            let result = getCollectionViewSize(count: count)
            
            collectionViewWidth.constant = result.pSize.width
            collectionViewHeight.constant = result.pSize.height
            
            //更新itemsize
            picFlowLayout.itemSize = result.itemSize
            //xcode 8.0以后 不会立即更行约束,配图视图的大小还是使用的上一个cell的尺寸
            picturesCollectionV.layoutIfNeeded()
            
            picturesCollectionV.items = itemModel?.imgUrls
            
            retweetText?.attributedText = itemModel?.retweetedText
            

        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
//        self.backgroundColor = arcColor()
        
        
        lblText.numberOfLines = 0
        lblText.preferredMaxLayoutWidth = kUISCRRENW - 2 * margin
        
        
        retweetText?.numberOfLines = 0
        retweetText?.preferredMaxLayoutWidth = kUISCRRENW - 2 * margin
        
        self.picFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        self.picFlowLayout.minimumLineSpacing = imgMargin
        self.picFlowLayout.minimumInteritemSpacing = imgMargin
        
        self.selectionStyle = .none
        
        //响应高亮部分点击事件
        //typedef void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect);
        lblText.highlightTapAction = { (containerView,text,range,rect) -> () in
            
            let subStr = (text.string as NSString).substring(with: range)
            
            if subStr.hasPrefix("http") {
                
                let vc = LEEWebViewController()
                
                vc.urlStr = subStr
                
               self.findNavigationController()?.pushViewController(vc, animated: true)
            }
        }
        retweetText?.highlightTapAction = { (containerView,text,range,rect) -> () in
            
            let subStr = (text.string as NSString).substring(with: range)
            
            if subStr.hasPrefix("http") {
                
                let vc = LEEWebViewController()
                
                vc.urlStr = subStr
                
            self.findNavigationController()?.pushViewController(vc, animated: true)
            }
        }
    }

   ///计算图片显示的尺寸
    private func getCollectionViewSize(count : Int) -> (pSize:  CGSize,itemSize: CGSize) {
        
        if count == 0 {
            
            return (CGSize(width: 1 ,height: 1) , CGSize(width: 1 ,height: 1))
        }
        if count == 1 {
            //计算图片的大小  获取图片  -> image.size  必须确保在这里一定能够获取到图片
            //不能够在这个地方下载图片 否则会执行多次刷新(有多少个单张图片就需要刷新多少次)
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: itemModel?.imgUrls?.last?.imgUrl ?? "") {
                
                let imageSize = image.size
                
                return (imageSize , imageSize)
            }
            return (CGSize(width: itemWidth, height: itemWidth) ,CGSize(width: itemWidth, height: itemWidth))
        }
        
        if count == 4 {
            //+1 是微博已经改版，现在是一张大图，3张小图
            let  W = itemWidth * 2 + imgMargin + 1
            
            return (CGSize(width: W, height: W) ,CGSize(width: itemWidth, height: itemWidth))
        }
        
        let row = CGFloat((count - 1) / 3 + 1)
        
        let H = row * itemWidth + CGFloat(count - 1) * imgMargin
        
        return (CGSize(width: maxWidth, height: H) ,CGSize(width: itemWidth, height: itemWidth))
        
    }
    

}
